import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { JhiEventManager, JhiAlertService } from 'ng-jhipster';

import { SubjectClazzMySuffix } from './subject-clazz-my-suffix.model';
import { SubjectClazzMySuffixPopupService } from './subject-clazz-my-suffix-popup.service';
import { SubjectClazzMySuffixService } from './subject-clazz-my-suffix.service';
import { TopSubjectMySuffix, TopSubjectMySuffixService } from '../top-subject-my-suffix';

@Component({
    selector: 'jhi-subject-clazz-my-suffix-dialog',
    templateUrl: './subject-clazz-my-suffix-dialog.component.html'
})
export class SubjectClazzMySuffixDialogComponent implements OnInit {

    subjectClazz: SubjectClazzMySuffix;
    isSaving: boolean;

    topsubjects: TopSubjectMySuffix[];

    constructor(
        public activeModal: NgbActiveModal,
        private jhiAlertService: JhiAlertService,
        private subjectClazzService: SubjectClazzMySuffixService,
        private topSubjectService: TopSubjectMySuffixService,
        private eventManager: JhiEventManager
    ) {
    }

    ngOnInit() {
        this.isSaving = false;
        this.topSubjectService.query()
            .subscribe((res: HttpResponse<TopSubjectMySuffix[]>) => { this.topsubjects = res.body; }, (res: HttpErrorResponse) => this.onError(res.message));
    }

    clear() {
        this.activeModal.dismiss('cancel');
    }

    save() {
        this.isSaving = true;
        if (this.subjectClazz.id !== undefined) {
            this.subscribeToSaveResponse(
                this.subjectClazzService.update(this.subjectClazz));
        } else {
            this.subscribeToSaveResponse(
                this.subjectClazzService.create(this.subjectClazz));
        }
    }

    private subscribeToSaveResponse(result: Observable<HttpResponse<SubjectClazzMySuffix>>) {
        result.subscribe((res: HttpResponse<SubjectClazzMySuffix>) =>
            this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError());
    }

    private onSaveSuccess(result: SubjectClazzMySuffix) {
        this.eventManager.broadcast({ name: 'subjectClazzListModification', content: 'OK'});
        this.isSaving = false;
        this.activeModal.dismiss(result);
    }

    private onSaveError() {
        this.isSaving = false;
    }

    private onError(error: any) {
        this.jhiAlertService.error(error.message, null, null);
    }

    trackTopSubjectById(index: number, item: TopSubjectMySuffix) {
        return item.id;
    }
}

@Component({
    selector: 'jhi-subject-clazz-my-suffix-popup',
    template: ''
})
export class SubjectClazzMySuffixPopupComponent implements OnInit, OnDestroy {

    routeSub: any;

    constructor(
        private route: ActivatedRoute,
        private subjectClazzPopupService: SubjectClazzMySuffixPopupService
    ) {}

    ngOnInit() {
        this.routeSub = this.route.params.subscribe((params) => {
            if ( params['id'] ) {
                this.subjectClazzPopupService
                    .open(SubjectClazzMySuffixDialogComponent as Component, params['id']);
            } else {
                this.subjectClazzPopupService
                    .open(SubjectClazzMySuffixDialogComponent as Component);
            }
        });
    }

    ngOnDestroy() {
        this.routeSub.unsubscribe();
    }
}
